/*
Copyright (C) 2015 Pieter Staal

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "biomeGenerator.h"
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace sf;

BiomeGenerator::BiomeGenerator(BiomeSettings settings)
{
    heightmap = settings.heightmap;
    landmap = settings.landmap;
    watermap = settings.watermap;
    groundWatermap = settings.groundWatermap;
    heatmap = settings.heatmap;
    windmap = settings.windmap;
    waterLevel = settings.waterLevel;
    this->settings = settings;
    //std::cout << settings.size << " size \n";

    biomemap = new Biome[settings.size * settings.size];
}

void BiomeGenerator::generateBiomes()
{
    //std::cout << settings.size << " size \n";
    for(int x = 0; x < settings.size; x++)
    {
        for(int y = 0; y < settings.size; y++)
        {
            int index = y * settings.size + x;
            biomemap[index] = getBiomeForPosition(x, y);
        }
    }
}

Biome BiomeGenerator::getBiomeForPosition(int x, int y)
{
    Biome biome = PLAINS;

    int index = x + y * settings.size;

    int altitude = heightmap[index] * scale - scale * waterLevel; // in meters above sea level
    //std::cout << "X: " << x << ", Y: " << y << " is " << altitude << " meters above sea level.\n";

    if (landmap[index] == false)
        biome =  OCEAN;
    else if (heightmap[index] < waterLevel)
        biome =  LAKE;
    else if (watermap[index] > 0)
        biome =  RIVER;
    else
    {
        bool freezing = heatmap[index] < 0.3;
        bool hot = heatmap[index] > 0.6;
        bool dry = groundWatermap[index] < 0.3;
        bool wet = groundWatermap[index] > 0.4;
        bool windy = windmap[index] > 0.5;

        if (altitude > 1750)
            biome =  ROCK;
        if (!dry && altitude > 2200)
            biome =  SNOW;
        if (windy && wet && freezing && altitude < 1750)
            biome = CONIFEROUS_FOREST;
        if (windy && wet && !hot && !freezing && altitude < 1250)
            biome = DECIDUOUS_FOREST;
        if (!windy && dry && altitude < 1750)
            biome =  DESERT;
        if (altitude < 50)
            biome =  BEACH;
    }

    return biome;
}

Biome* BiomeGenerator::getBiomeMap()
{
    return this->biomemap;
}

Image BiomeGenerator::getImage()
{
    Image image;
    image.create(settings.size, settings.size);
    for (int x = 0; x < settings.size; x++)
    {
        for (int y = 0; y < settings.size; y++)
        {
            image.setPixel(x, y, biomeColors[biomemap[y * settings.size + x]]);
        }
    }
    return image;
}

Image BiomeGenerator::getRawImage()
{
    Image image;
    image.create(settings.size, settings.size);
    for (int x = 0; x < settings.size; x++)
    {
        for (int y = 0; y < settings.size; y++)
        {
            int value = biomemap[y * settings.size + x];
            image.setPixel(x, y, Color(value, value, value));
        }
    }
    return image;
}




